package com.bangdream.michelia.view.activity.exam;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.ExamContract;
import com.bangdream.michelia.entity.exam.ExamBean;
import com.bangdream.michelia.entity.exam.ExamQuestBean;
import com.bangdream.michelia.entity.exam.ExamResultDetailsBean;
import com.bangdream.michelia.entity.exam.ExamResultItemBean;
import com.bangdream.michelia.entity.exam.SubjectBean;
import com.bangdream.michelia.entity.exam.SubjectInfoBean;
import com.bangdream.michelia.presenter.ExamPresenter;
import com.bangdream.michelia.tool.GridSpacingItemDecoration;
import com.bangdream.michelia.utils.PublicUtil;
import com.bangdream.michelia.view.activity.base.SwipeBackActivity;
import com.bangdream.michelia.view.adapter.ExamResultAdapter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.DoubleAccumulator;

/**
 * @date on 2018/8/19 1:36
 * @email 909732184@qq.com
 * 考试结果页
 */
public class ExamResultActivity extends SwipeBackActivity<ExamContract.IExamView,ExamPresenter<ExamContract.IExamView>>
implements ExamContract.IExamView{
    private TextView tvNumber,tvTime,tvCorrectRate,checkAnswer;
    private ImageView ivType;
    private RecyclerView recyclerView;
    private GridLayoutManager gridLayoutManager;
    private ExamResultAdapter adapter;
    private ExamResultDetailsBean data;
    private ExamBean bean;
    private RelativeLayout layoutBg;
    private String examFinishId;
    @Override
    public int getLayout() {
        return R.layout.activity_exam_result;
    }

    @Override
    public void findId() {
        recyclerView = findViewById(R.id.recyclerView);
        layoutBg = findViewById(R.id.layoutBg);
        tvNumber = findViewById(R.id.tvNumber);
        tvTime = findViewById(R.id.tvTime);
        ivType = findViewById(R.id.ivType);
        tvCorrectRate = findViewById(R.id.tvCorrectRate);
        checkAnswer = findViewById(R.id.checkAnswer);
    }

    @Override
    public void setListener() {
        adapter.setOnViewItemClickListener(position -> {
            Intent intent = new Intent(ExamResultActivity.this,AnswerAnalysisActivity.class);
            Bundle b = new Bundle();
            b.putInt("position",position);
            b.putParcelableArrayList("ExamResultItemBean", (ArrayList<? extends Parcelable>) adapter.getData());
            intent.putExtras(b);
            startActivity(intent);
        });
        checkAnswer.setOnClickListener(view -> {
            if(adapter.getData().size()>0){
                Intent intent = new Intent(ExamResultActivity.this,AnswerAnalysisActivity.class);
                Bundle b = new Bundle();
                b.putInt("position",0);
                b.putParcelableArrayList("ExamResultItemBean", (ArrayList<? extends Parcelable>) adapter.getData());
                intent.putExtras(b);
                startActivity(intent);
            }
        });

    }

    @Override
    public void logic() {
        initTitleBar(" ","最新考试结果","",this);
        if(getIntent().getExtras()!=null){
            bean = getIntent().getExtras().getParcelable("data");
            if(bean==null){
                examFinishId = getIntent().getExtras().getString("examId");
            }
        }
        //网格布局分为两列
        gridLayoutManager = new GridLayoutManager(this,7);
        adapter = new ExamResultAdapter(this);
        recyclerView.setLayoutManager(gridLayoutManager);

        //设置每个item间距
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(7, PublicUtil.dip2px(this,10),false));
        recyclerView.setAdapter(adapter);
        if(bean!=null&&bean.getId()!=null) {
            mPresenter.getExamResultDetails(bean.getExamIdFinish(),pd);
            mPresenter.getExamResultList(bean.getExamIdFinish(),null);
        }
        if(examFinishId!=null&&!examFinishId.equals("")){
            mPresenter.getExamResultDetails(examFinishId,pd);
            mPresenter.getExamResultList(examFinishId,null);
        }
    }
    //b8d4efdfaa2640f2aa9fc9f87304656b
//29bdbcfc478f4442a5f68ddcc52df24d
    @Override
    public ExamPresenter createPresenter() {
        return new ExamPresenter();
    }

    @Override
    public void setExamList(boolean isSuccess, int page, List<ExamBean> data, String stDescribe) {

    }

    @Override
    public void setSubjectList(boolean isSuccess, List<SubjectBean> data, String stDescribe) {

    }

    @Override
    public void setSubjectDetails(boolean isSuccess, SubjectInfoBean data, String stDescribe) {

    }

    @Override
    public void initExamId(boolean isSuccess, String examId, String stDescribe) {

    }

    @Override
    public void actionSubmitExam(boolean isSuccess, boolean isFinish, String stDescribe) {

    }

    @Override
    public void actionSubmitQuest(boolean isSuccess, boolean isFinish, String stDescribe) {

    }

    @Override
    public void setExamResultList(boolean isSuccess, List<ExamResultItemBean> data, String stDescribe) {
        adapter.setData(data);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void setExamResultDetails(boolean isSuccess, ExamResultDetailsBean data, String stDescribe) {
        if(isSuccess){
            if(data!=null){
                this.data = data;
                setViewData();
            }
        }
    }

    @Override
    public void setExamQuestList(boolean isSuccess, List<ExamQuestBean> data, String stDescribe) {

    }

    @Override
    public void setExamQuList(boolean isSuccess, List<ExamBean> data, String stDescribe) {

    }

    private void setViewData(){
        if(data==null)
            return;
        //考试通过
        if(data.getIsPassed()!=null&&data.getIsPassed().equals("1")){
            layoutBg.setBackground(ContextCompat.getDrawable(this,R.mipmap.icon_kaoshi_result_bg2));
            ivType.setImageResource(R.mipmap.icon_kaoshi_result_b);
        }else {
            layoutBg.setBackground(ContextCompat.getDrawable(this,R.mipmap.icon_kaoshi_result_bg1));
            ivType.setImageResource(R.mipmap.icon_kaoshi_result_a);
        }
        tvNumber.setText(data.getScore()+"");
        tvTime.setText(data.getTestDate()+"");
//        tvCorrectRate.setText(data.getAccuracy()+"");
        //保留两位小数
        Double d = data.getAccuracy()*100;
//        double d = Double.parseDouble(data.getAccuracy());
        BigDecimal b = new BigDecimal(d);
        double df = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        tvCorrectRate.setText(df+"%");
    }
}
